package com.skywolf.chem.match.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.skywolf.chem.common.config.data.DataSource;
import com.skywolf.chem.common.config.data.DataSourceNames;
import com.skywolf.chem.common.sysconfig.pojo.vo.PageVO;
import com.skywolf.chem.match.common.page.PageInfo;
import com.skywolf.chem.match.entity.Info;
import com.skywolf.chem.match.entity.Sponsor;
import com.skywolf.chem.match.mapper.MatchInfoMapper;
import com.skywolf.chem.match.mapper.SponsorMapper;
import com.skywolf.chem.match.params.page.MatchInfoPageParam;
import com.skywolf.chem.match.params.param.ApplyUserInfoParam;
import com.skywolf.chem.match.params.vo.MatchInfoQueryVo;
import com.skywolf.chem.match.params.vo.MatchPageInfoQueryVo;
import com.skywolf.chem.match.params.vo.OrderListQueryVo;
import com.skywolf.chem.match.service.IMatchInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.skywolf.chem.match.utils.FastJsonUtils;
import com.skywolf.chem.match.utils.StringUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 赛事详情 服务实现类
 * </p>
 *
 * @author
 * @since 2021-09-24
 */
@Service
@AllArgsConstructor
public class MatchInfoServiceImpl extends ServiceImpl<MatchInfoMapper, Info> implements IMatchInfoService {

    private final MatchInfoMapper matchInfoMapper;
    private final SponsorMapper matchSponsorMapper;

    @Override
    @DataSource(DataSourceNames.TWO)
    public PageVO<MatchPageInfoQueryVo> getMatchQueryList(MatchInfoPageParam pageParam) {
        Page<MatchPageInfoQueryVo> page = new PageInfo<>(pageParam);
        IPage<MatchPageInfoQueryVo> iPage = matchInfoMapper.getMatchQueryList(page, pageParam);
        return new PageVO<MatchPageInfoQueryVo>(iPage);
    }

    @Override
    @DataSource(DataSourceNames.TWO)
    public MatchInfoQueryVo getMatchInfoById(Long id, Long userId) {
        MatchInfoQueryVo vo = matchInfoMapper.getMatchInfoById(id, userId);
        List<Sponsor> sponsorList = matchSponsorMapper.selectList(new LambdaQueryWrapper<Sponsor>().eq(Sponsor::getMatchId, id));
        if (null != sponsorList && sponsorList.size() > 0) {
            List<MatchInfoQueryVo.SponsorQueryVo> sponsorQueryVoList = sponsorList.parallelStream().map(item -> {
                MatchInfoQueryVo.SponsorQueryVo sponsorQueryVo = new MatchInfoQueryVo.SponsorQueryVo();
                sponsorQueryVo.setSponsor(item.getSponsor());
                sponsorQueryVo.setPhone(item.getPhone());

                return sponsorQueryVo;
            }).collect(Collectors.toList());
            vo.setSponsorList(sponsorQueryVoList);
        }
        if (StringUtils.isNotBlank(vo.getCustomTag()) || "[]".equals(vo.getCustomTag())) {
            vo.setCustomTagList(FastJsonUtils.toList((JSON.parse(vo.getCustomTag()).toString()), ApplyUserInfoParam.ApplyUserCustomTagModel.class));
        }
        return vo;
    }

    @Override
    @DataSource(DataSourceNames.TWO)
    public List<OrderListQueryVo> getCollectQueryVoByUserId(Long userId) {
        return matchInfoMapper.getCollectQueryVoByUserId(userId);
    }

    @Override
    @DataSource(DataSourceNames.TWO)
    public Info getById(Long id) {
        return this.getById(id);
    }
}
